#include "ScreenWorld.h"
	ScreenWorld::ScreenWorld(int32 w,int32 h)
		:Manager("Menu"),
		Component("ScreenWorld"),
		m_screenWidth(w),
		m_screenHeight(h)
	{
	}

	ScreenWorld::~ScreenWorld(void)
	{
	}

	void ScreenWorld::addedToObject()
	{
		requestMessage("Init",&ScreenWorld::Init);
		requestMessage("InitFightData",&ScreenWorld::InitFightMenu);
		requestMessage("RenderScreenWorld",&ScreenWorld::Render);
		requestMessage("UpdateCursor",&ScreenWorld::UpdateMousePos);
		requestMessage("AddBattleMenu",&ScreenWorld::AddBattle);
		requestMessage("DeleteBattleMenu",&ScreenWorld::DeleteBattle);
		requestMessage("ClickedMenu",&ScreenWorld::ClickedMenu);
	}

	void ScreenWorld::Init(const Message &msg)
	{
		m_cursor=-1;
		m_bradius=-1;
		sendMessage("LoadSysMenu");
		D3DXCreateSprite(g_pd3dDevice,&m_pSprite); 
	}

	void ScreenWorld::InitFightMenu(const Message &msg)
	{
		sendMessage("LoadFightMenu");
	}

	void ScreenWorld::ClickedMenu(const Message & msg)
	{
		vector< boost::any >* tt=boost::any_cast<vector<boost::any>*>(msg.p);
		vector< boost::any >::iterator iter=tt->begin();
		D3DXVECTOR2 pos=boost::any_cast<D3DXVECTOR2>(*iter++);
		FIGHT_MENU* fm=boost::any_cast<FIGHT_MENU*>(*iter);
		for(vector<PICTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
		{
			if(!iter->draw)
				continue;
			if(iter->tid>=BATTLE_DISK&&iter->tid<=BATTLE_PROPS)
			{
				uint32 w=iter->info.Width;
				uint32 h=iter->info.Height;
				if(pos.x>=iter->pos.x&&pos.x<=(iter->pos.x+w)
					&&pos.y>=iter->pos.y&&pos.y<=(iter->pos.y+h))
				{
					switch(iter->tid)
					{
					case BATTLE_ATTACK:
						sendMessage("ClickedAttack");
						*fm=MATTACK;
						return;
						break;
					case BATTLE_COMSKILL:
						sendMessage("ClickedSkill");
						*fm=MCOMSKILL;
						return;
						break;
					case BATTLE_GUARD:
						sendMessage("ClickedGuard");
						*fm=MGUARD;
						return;
						break;
					case BATTLE_MAGIC:
						sendMessage("ClickedMagic");
						*fm=MMAGIC;
						return;
						break;
					case BATTLE_MOVE:
						DeleteBattle();
						sendMessage("ClickedMove");
						*fm=MMOVE;
						return;
						break;
					case BATTLE_PROPS:
						sendMessage("ClickedPROPS");
						*fm=MPROPS;
						return;
						break;
					default:
						break;
					}
				}
			}
		}
		DeleteBattle();
		*fm=NONE;
		return;
	}

	bool ScreenWorld::HasObj(int32 id)
	{
		for(vector<PICTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
		{
			if(iter->tid==id)
				return true;
		}
		return false;
	}

	bool ScreenWorld::AddFightMenu(int32 id,char* fname)
	{
		int32 _id;
		if((_id=AddMenu(id,fname))<=0)
			return false;
		if(id==BATTLE_DISK)
			m_bradius=m_data[_id].info.Width/2-7;
		return true;
	}

	int32 ScreenWorld::AddMenu(int32 id,char* fname)
	{
		if(HasObj(id))
			return -1;
		PICTURE tmp;
		tmp.id=m_cur++;
		tmp.tid=id;
		tmp.draw=false;
		tmp.pos=D3DXVECTOR2(0,0);
		if(FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice,fname, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,0,D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0xFF000000,&tmp.info,NULL,&tmp.tex)))
		{
			return -1;
		}
		m_data.push_back(tmp);
		return tmp.id;
	}

	bool ScreenWorld::AddSysMenu(int32 id,char* fname)
	{
		int32 _id;
		if((_id=AddMenu(id,fname))<0)
			return false;
		if(id==CURSOR)
			m_cursor=_id;
		return true;
	}


	void ScreenWorld::AddBattle(const Message & msg)
	{
		D3DXVECTOR3 pos=boost::any_cast<D3DXVECTOR3>(msg.p);
		m_battlePos=pos;
		D3DXVECTOR2 spos=Coordinate::Instance()->WorldToScreenCoord(&m_battlePos);
		UpdateBattle(spos);
	}

	void ScreenWorld::DeleteBattle()
	{
		for(vector<PICTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
		{
			if(iter->tid>=BATTLE_DISK&&iter->tid<=BATTLE_PROPS)
			{
				iter->draw=false;
			}
		}
	}

	void ScreenWorld::DeleteBattle(const Message & msg)
	{
		DeleteBattle();
	}

	bool ScreenWorld::UpdateBattle()
	{
		/*if(m_battlePos==D3DXVECTOR3(-1000.0f,-1000.0f,-1000.0f))
		return false;
		D3DXVECTOR2 spos=m_coord->WorldToScreenCoord(&m_battlePos);
		UpdateBattle(spos);*/
		return true;
	}

	bool ScreenWorld::UpdateBattle(D3DXVECTOR2 pos)
	{

		for(vector<PICTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
		{
			if(iter->tid>=1000&&iter->tid<2000)
			{
				switch(iter->tid)
				{
				case BATTLE_DISK:
					iter->pos=D3DXVECTOR2(pos.x-iter->info.Width/2,pos.y-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_ATTACK:
					iter->pos=D3DXVECTOR2(pos.x+(m_bradius-iter->info.Width/2),pos.y-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_COMSKILL:
					iter->pos=D3DXVECTOR2(pos.x+(m_bradius/2-iter->info.Width/2),pos.y-m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_GUARD:
					iter->pos=D3DXVECTOR2(pos.x-(m_bradius/2+iter->info.Width/2),pos.y-m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_MAGIC:
					iter->pos=D3DXVECTOR2(pos.x+(m_bradius/2-iter->info.Width/2),pos.y+m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_MOVE:
					iter->pos=D3DXVECTOR2(pos.x-(m_bradius/2+iter->info.Width/2),pos.y+m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_PROPS:
					iter->pos=D3DXVECTOR2(pos.x-(m_bradius+iter->info.Width/2),pos.y-iter->info.Height/2);
					iter->draw=true;
					break;
				default:
					break;
				}
			}
		}
		return true;
	}

	bool ScreenWorld::UpdatePos(int32 id,D3DXVECTOR2 pos)
	{
		if(m_data.size()<=id)
			return false;
		m_data[id].pos=pos;
		return true;
	}

	bool ScreenWorld::DrawPic(int32 id)
	{
		if(m_data.size()<=id)
			return false;
		m_data[id].draw=true;
		return true;
	}

	bool ScreenWorld::UnDrawPic(int32 id)
	{
		if(m_data.size()<=id)
			return false;
		m_data[id].draw=false;;
		return true;
	}

	void ScreenWorld::UpdateMousePos(const Message &msg)
	{
		D3DXVECTOR2 pos=boost::any_cast<D3DXVECTOR2>(msg.p);
		if(m_cursor==-1)
		{
			return;
		}
		UpdatePos(m_cursor,pos);
		DrawPic(m_cursor);
	}

	HRESULT ScreenWorld::RenderScreenWorld()
	{
		for(vector<PICTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
		{
			if(!iter->draw||iter->id==m_cursor)
				continue;
			m_pSprite->Draw(iter->tex,NULL,NULL,&D3DXVECTOR3(iter->pos.x,iter->pos.y,0.0f),0xffffffff);
		}

		m_pSprite->Draw(m_data[m_cursor].tex,NULL,NULL,&D3DXVECTOR3(m_data[m_cursor].pos.x,m_data[m_cursor].pos.y,0.0f),0xffffffff);
		return S_OK;
	}

	void ScreenWorld::Render(const Message &msg)
	{
		g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
		if(SUCCEEDED(m_pSprite->Begin(0)))
		{
			RenderScreenWorld();
			m_pSprite->End();
		}
		g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
	}

	void ScreenWorld::Log()
	{
	}